/**
 * Created by Administrator on 2018/10/17 0017.
 */

//获取分页信息
function getAuthListPage(pageurl,keyword,auth_id,auth_level,curr){
    $("#allChk").prop("checked",false);
    sign.params={
        auth_level:auth_level,
        auth_pid:auth_id,
        keyword:keyword,
        curr:curr
    };
    var encode_post = sign.encode_post();
    layui.use(['laypage','form', 'layer'], function(){
        var laypage = layui.laypage
            ,layer = layui.layer
            ,form = layui.form;
        var index = layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});

        $.post(pageurl,encode_post, function(data){
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }

            datass = sign.decode_post(data);

            //选择
            auth_list(datass);

            form.render('checkbox');
            layer.close(index);
            if(datass.data.page<=1){
                $('#pages').html('');
            }
            else {
                //显示分页
                laypage.render({
                    elem: 'pages', 			// 容器
                    count: datass.data.total,     	// 总页数(后台的)
                    curr: datass.data.curr, 		//当前页(后台获取到的)
                    prev: '&laquo;',
                    next: '&raquo;',
                    jump: function (obj, first) {
                        if (!first) {
                            getAuthListPage(pageurl,keyword,auth_id,auth_level,obj.curr);
                        }
                    }
                });
            }
        });
    });
}
//获取替换信息
function auth_list(data) {
    var html='';
    if (data['data']['list'] === undefined || data['data']['list'].length == 0) {
        html ='<tr><td align="center" colspan="12">暂无相关数据</td></tr>';
        //页面数据替换
        $('#auth_list').html(html);
    }
    else {
        list = data['data']['list'];
        for (var i in list) {
            list[i].auth_id = list[i].auth_id ? list[i].auth_id : '';
            list[i].auth_pid = list[i].auth_pid ? list[i].auth_pid : '';
            list[i].auth_c = list[i].auth_c ? list[i].auth_c : '';
            list[i].auth_a = list[i].auth_a ? list[i].auth_a : '';
            list[i].auth_level = list[i].auth_level ? list[i].auth_level +1: 1;


            html += '<tr> <td class="tc"><input name="id[]" value="' + list[i].auth_id + '" type="checkbox"></td> <td>';
            html += list[i].auth_name + '</td>';
            html += '<td>' + list[i].auth_c + '</td>';

            html += '<td>'+list[i].auth_a+'</td>';
            html += '<td class="link-update">'+list[i].auth_level+'</td><td class="link-update">';
            html += '<a class="link-update" href="#" onclick="auth_oncli('+list[i].auth_id+',\''
                +list[i].auth_name+'\',\''+list[i].auth_c+'\',\''+list[i].auth_a+'\',\''+list[i].auth_pid+'\')">';
            html += '修改</a>';
            html += '<a class="link-del" href="#" onclick="auth_del(' + list[i].auth_id + ')">删除</a>';
            html += '</td></tr>';
        }
        $('#auth_list').html(html);
    }
}



//打开 修改弹出层权限信息
function auth_oncli(id,name,c,a,pid) {
    //替换弹出层的信息
    document.getElementById('a_id').value = id;
    document.getElementById('auth_name').value = name;
    document.getElementById('auth_c').value = c;
    document.getElementById('auth_a').value = a;
    if(pid!=''&& pid!=null){
        document.getElementById('auth_pid').value = pid;
    }else {
        document.getElementById('auth_pid').value = '';
    }

    $('.bgPop,.pop').show();
}
//关闭
function auth_outcli() {
    $('.bgPop,.pop').hide();
}
//修改弹出层权限信息
function auth_update() {
    var auth_id = $('#a_id').val();
    var auth_name = $('#auth_name').val();
    var auth_pid = $('#auth_pid').val();
        auth_pid = auth_pid?auth_pid:0;
    var auth_c = $('#auth_c').val();
    var auth_a = $('#auth_a').val();
    console.log(auth_pid);
    sign.params={
        auth_id :auth_id,
        auth_name:auth_name,
        auth_pid:auth_pid,
        auth_c:auth_c,
        auth_a:auth_a
    };
    var encode_post = sign.encode_post();
    $.post(url_list.auth_update,encode_post,function (data) {
        //解码
        if(typeof data == 'string'){
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        console.log(data);
        if(data.code==200){
            layer.msg(data.msg, {icon: 1},1500);//提示

            var auth_idd = $('#auth_id').val() ||$('#auth_ids').val();
            var auth_level = $('#access_level').val();

            getAuthListPage(url_list.auth_list,keyword,auth_idd,auth_level);//更新列表
            auth_outcli();//关闭弹窗
        }else {
            layer.msg(data.msg, {icon: 2},1500);
        }
    })
}


//添加权限弹窗
function auth_add(){
    //弹窗
    layer.prompt({
        formType: 2,
        title: '请输入权限名称',
        area: ['400px', '50px'] //自定义文本域宽高
    }, function(value, index, reply_content){
        sign.params={
            auth_name:value
        };
        var encode_data = sign.encode_post();
        $.post(url_list.auth_add,encode_data,function (data) {
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }
            data = sign.decode_post(data);
            console.log(data);
            if (data.code == 200) {
                layer.msg(data.msg, {icon: 1, time: 1500});
                var auth_idd = $('#auth_id').val() ||$('#auth_ids').val();
                var auth_level = $('#access_level').val();

                getAuthListPage(url_list.auth_list,keyword,auth_idd,auth_level);//更新列表
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        });
        layer.close(index);
    });
}


//删除
function auth_del(id) {
    sign.params={
        auth_id:id,
        del:0
    };
    var encode_post = sign.encode_post();
    if (confirm("确定要删除？")) {
        $.post(url_list.auth_update, encode_post, function (data) {
            if (typeof data == 'string') {
                data = JSON.parse(data);
            }
            data = sign.decode_post(data);
            console.log(data);
            if (data.code == 200) {
                layer.msg(data.msg, {icon: 1}, 1500);
                var auth_idd = $('#auth_id').val() ||$('#auth_ids').val();
                var auth_level = $('#access_level').val();

                getAuthListPage(url_list.auth_list,keyword,auth_idd,auth_level);//更新列表
                auth_outcli();
            } else {
                layer.msg(data.msg, {icon: 2}, 1500);
            }

        });
    }
}
//批量删除
function auth_dels() {
    var checkedNum = $("input[name='id[]']:checked").length;
    if (checkedNum == 0) {
        layer.msg('请选择至少一项', {icon: 2});
        return;
    }
    // 批量选择
    if (confirm("确定要删除？")) {
        var checkedList = new Array();

        $("input[name='id[]']:checked").each(function () {
            checkedList.push($(this).val());
        });
    }
    var check_id =checkedList.join(',');
    sign.params={
        auth_id :check_id,
        del:0
    };
    var encode_post = sign.encode_post();
    $.post(url_list.auth_update,encode_post,function (data) {
        if (typeof data == 'string') {
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        console.log(data);
        if (data.code == 200) {
            layer.msg(data.msg, {icon: 1}, 1500);
            var auth_idd = $('#auth_id').val() ||$('#auth_ids').val();
            var auth_level = $('#access_level').val();

            getAuthListPage(url_list.auth_list,keyword,auth_idd,auth_level);//更新列表
        } else {
            layer.msg(data.msg, {icon: 2}, 1500);
        }
    })

}

//全选
$("#allChk").click(function() {
    $("input[name='id[]']").prop("checked",this.checked);
});

